package io.github.uicdb.et.block;

import io.github.uicdb.et.ModMenuAndScreens;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.SimpleContainerData;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

@Getter
public class TurnInMenu extends AbstractContainerMenu {
    private final Container container;
    private final ContainerData elementContainerData;
    private final ContainerData recipeContainerData;
    private final BlockPos pos;
    public TurnInMenu(int containerId, Inventory playerInventory, Container container, ContainerData elementContainerData, ContainerData recipeContainerData, FriendlyByteBuf buf) {
        super(ModMenuAndScreens.TURN_IN.get(), containerId);
        this.container = container;
        this.elementContainerData = elementContainerData;
        this.recipeContainerData = recipeContainerData;
        this.addDataSlots(elementContainerData);
        this.addDataSlots(recipeContainerData);
        container.startOpen(playerInventory.player);
        addSlot(new Slot(container, 0, 8, 76){
            @Override
            public boolean mayPlace(@NotNull ItemStack stack) {
                return recipeContainerData.get(2)==0;//配方开始时锁定输入输出
            }

            @Override
            public boolean mayPickup(@NotNull Player player) {
                return recipeContainerData.get(2)==0;
            }
        });
        addSlot(new ResultSlot(container, 1, 152, 76));
        addPlayerInventory(playerInventory, 8, 114);
        this.pos = buf.readBlockPos();
    }
    public TurnInMenu(int containerId, Inventory playerInventory, FriendlyByteBuf buf) {
        this(containerId,playerInventory,new SimpleContainer(2),new SimpleContainerData(7),new SimpleContainerData(3),buf);
    }
    private void addPlayerInventory(Inventory playerInventory, int x, int y) {
        for (int yIndex = 0; yIndex < 3; yIndex++) {
            for (int xIndex = 0; xIndex < 9; xIndex++) {//9-35
                this.addSlot(new Slot(playerInventory, xIndex + yIndex * 9 + 9, x + xIndex * 18, y + yIndex * 18 ));
            }
        }

        for (int xIndex = 0; xIndex < 9; xIndex++) {//0-8
            this.addSlot(new Slot(playerInventory, xIndex, x + xIndex * 18, y + 58));
        }
    }

    @Override
    public ItemStack quickMoveStack(Player player, int slot) {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean stillValid(Player player) {
        return true;
    }

}
